var mmm;
(function () {
    window.CaptureInfor = {
        uploadInfor: {
            "app_key": "",
            "body": {
                "accessId":'',
                "activityCode": "",
                "activityCodeId": "",
                "activityName": "",
                "bookTime": "",
                "channel": "",
                "confirmTime": "",
                "confirmWay": "",
                "ordCreateTime": "",
                "picAfterUrl": "",
                "picBeforeUrl": "",
                "picBeingUrl": "",
                "proofContent": "",
                "reconfirmLog": "",
                // "resultStatus":'1',//成功状态：1成功，2失败
                "successfulContent": "",
                "tid": "",
            },
            "platform": "H5",
            "signature": "1343242342",
            "timestamp": "",
            "state": 0,
        },
        pubicPath: 'https://ldwxmc.com/CrbtByProvice/ShanXi',//测试地址
        timeUrl: '/sysDateTime', //获取当前服务端时间
        uploadUrl: '/savePic',//凭证提交
        tidUrl: '/certificateV3/getToken',//获取tid参数
        level: 1,
        uuid: function (len, radix) {
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [],
                i;
            radix = radix || chars.length;

            if (len) {
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
            } else {
                var r;

                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';

                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random() * 16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }

            return uuid.join('');
        },
        setStrageInfor: function () {
            localStorage.uploadInfor = JSON.stringify(this.uploadInfor);
        },
        getStrageInfor: function () {
            if (localStorage.getItem("uploadInfor") && localStorage.getItem("uploadInfor").length > 0) {
                this.uploadInfor = JSON.parse(localStorage.getItem("uploadInfor"))
            }
        },
        removeStrageInfor: function () {
            localStorage.removeItem('uploadInfor');
        },
        createParam: function () {
            var option = {"body": {}};
            option = JSON.parse(JSON.stringify(this.uploadInfor));
            option.timestamp = Math.floor(new Date().getTime() / 1000).toString();
            return option;
        },
        getTimeApi: function (state) {
            var _this = this, time;
            $.ajax({
                type: 'get',
                url: _this.pubicPath + _this.timeUrl,

                success: function (data) {
                    if (data.state === "success") {
                        time = data.data;

                        if (state === 1) {
                            _this.uploadInfor.body.bookTime = time;
                        } else if (state === 2) {
                            _this.uploadInfor.body.confirmTime = time;
                        } else if (state === 3) {
                            _this.uploadInfor.body.ordCreateTime = time;
                            _this.level++;
                            if (_this.level === 3) _this.uploadApi(_this.createParam());
                            return;
                        }
                        _this.setStrageInfor();
                    }

                },

                error: function () {
                    if (state === 4) {
                        _this.level++;
                        if (_this.level === 3) _this.uploadApi(_this.createParam());
                    }
                }

            });
        },
        getTidApi: function (loginType,mobile,hncmjsSSOCookie) {
            var _this = this, tidType;
            _this.removeStrageInfor();
            var param={"body": {}};
            param.timestamp=Math.floor(new Date().getTime() / 1000).toString()
            param.app_key = _this.uploadInfor.app_key;
            param.platform = _this.uploadInfor.platform;
            param.signature = _this.uploadInfor.signature;
            if(loginType){//登录方式（9一键登录，2服务密码，3短信登录，10微信登录）
                param.body.loginType = loginType
            }
            if(mobile){//登录手机号码
                param.body.mobile = mobile
            }
            if(hncmjsSSOCookie){
                param.body.hncmjsSSOCookie = hncmjsSSOCookie
            }
            param.body.timeStr = _this.dateFormat(new Date(),'yyyyMMddhhmmss')
            $.ajax({
                type: 'POST',
                url: _this.pubicPath + _this.tidUrl,
                data: JSON.stringify(param),
                dataType: "json",
                contentType: 'application/json',
                success: function (data) {
                    if (data.code === "1") {
                        tidType = data.data;
                        _this.uploadInfor.body.tid = tidType;
                        _this.setStrageInfor();
                    }
                },
                error: function () {}
            });
            return tidType
        },
        uploadApi: function (option) {
            var _this = this;
            var param = option || _this.uploadInfor;
            _this.removeStrageInfor();
            _this.level = 1
            _this.uploadInfor.state=0
            $.ajax({
                type: 'POST',
                url: _this.pubicPath + _this.uploadUrl,
                data: JSON.stringify(param),
                dataType: "json",
                //测试跨域
                beforeSend:function(){
                    mmm =arguments;
                    console.log(111);
                },
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    console.log(100)
                }
            })
        },

        prtScr: function (selector, state) {
            var ele, _this = this;
            selector ? ele = document.querySelectorAll(selector)[0] : ele = document.body;
            var y = $('html').scrollTop() || $('body').scrollTop();
            html2canvas(ele, {
                WINDOWHEIGHT: document.body.scrollHeight,
                allowTaint: true,
                width: $(ele).width(),
                height: $(ele).height(),
                x: 0,
                y: 0,
                scrollY: y,
                scale: 1,
                useCORS: true
            }).then(function (canvas) {
                if($(ele).width()<600){
                    var imgUri = canvas.toDataURL("image/jpeg", 0.5); // 获取生成的图片的url
                }else if($(ele).width()<1000){
                    var imgUri = canvas.toDataURL("image/jpeg", 0.3); // 获取生成的图片的url
                }else{
                    var imgUri = canvas.toDataURL("image/jpeg", 0.2); // 获取生成的图片的url
                }
                if (state === 1) {
                    _this.uploadInfor.body.picBeforeUrl = imgUri;
                } else if (state === 2) {
                    _this.uploadInfor.body.picBeingUrl = imgUri;
                } else if (state === 3) {
                    _this.uploadInfor.body.picAfterUrl = imgUri;
                    _this.setStrageInfor();
                    _this.level++;
                    if (_this.level === 3) {
                        if (_this.level === 3) _this.uploadApi();
                    }
                    return;
                }
                _this.setStrageInfor();
            });
        },
        ScreenCapture: function (selector, option) {
            option = option || {};
            var read, upload;
            if (!!option.read) this.getStrageInfor();
            for (var item in option) {
                var s = item.trim();
                if (s === "pubicPath") {
                    this.pubicPath = option[s];
                    continue;
                }
                if (s === "read") {
                    read = option.read || false;
                    continue;
                }
                if (s === "upload") {
                    upload = option.upload || false;
                    continue;
                }
                if (s === "platform" || s === "app_key" || s === "signature" || s==="state") {
                    this.uploadInfor[s] = option[s];
                    continue;
                }
                this.uploadInfor.body[s] = option[s];
            }

            var _this = this, state = ++this.uploadInfor.state;

            if (upload) state = 4;
            if(state == 1){
                _this.setStrageInfor();
                _this.getTimeApi(state);
                _this.prtScr(selector, state);
            }else if(state == 2){
                _this.setStrageInfor();
                _this.getTimeApi(state);
                _this.prtScr(selector, state);
            }else if(state == 3){
                this.uploadInfor.timestamp = Math.floor(new Date().getTime() / 1000).toString();
                //_this.uploadApi();
                _this.getTimeApi(state);
                _this.prtScr(selector, state);
            }
        },
        //格式化日期
        dateFormat: function dateFormat(obj, fmt) {
            var o = {
                "M+": obj.getMonth() + 1,
                "d+": obj.getDate(),
                "h+": obj.getHours(),
                "m+": obj.getMinutes(),
                "s+": obj.getSeconds(),
                "q+": Math.floor((obj.getMonth() + 3) / 3),
                "S+": obj.getMilliseconds()
            };
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (obj.getFullYear() + "").substr(4 - RegExp.$1.length))
            }
            for (var k in o) {
                if (new RegExp("(" + k + ")").test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((((RegExp.$1.length == 3 && k == "S+") ? "000" : "00") + o[k]).substr(("" + o[k]).length)))
                }
            }
            return fmt
        }
    };

})();

